package com.yungu.swift.admin.route.common;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.config.ApplicationConfig;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.system.sys.model.dto.SysAreaDto;
import com.yungu.swift.system.sys.model.dto.SysCityDto;
import com.yungu.swift.system.sys.model.dto.SysProvinceDto;
import com.yungu.swift.system.sys.service.SysAreaService;
import com.yungu.swift.system.sys.service.SysCityService;
import com.yungu.swift.system.sys.service.SysProvinceService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author : cuixiuyin
 * @date : 2019/6/15
 */
@Slf4j
@RestController
@RequestMapping("/admin/v1/common/area")
@Api(value = "【通用服务】", tags = "省/市/县级别操作")
public class AreaAdmin {

    @Reference
    private SysProvinceService sysProvinceService;
    @Reference
    private SysCityService sysCityService;
    @Reference
    private SysAreaService sysAreaService;

    @RequestMapping(value = "/province", method = RequestMethod.GET)
    @ApiOperation(value = "获取全量的省市数据", httpMethod = "GET")
    public ResponseData<List<SysProvinceDto>> province() {
        return sysProvinceService.list(MapUtils.build(2));
    }

    @RequestMapping(value = "/cityByProvinceId", method = RequestMethod.GET)
    @ApiOperation(value = "根据省份id获取城市数据", httpMethod = "GET")
    public ResponseData<List<SysCityDto>> findCityByProvinceId(
            @ApiParam(value = "省份id", required = true) @RequestParam String provinceId,
            @ApiParam(value = "是否过滤城市中的区县数据，默认不过滤") @RequestParam(defaultValue = "false") Boolean filterArea) {
        if (ParamUtil.isIllegal(provinceId)) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return sysCityService.findCityByProvinceId(provinceId, filterArea);
    }

    @RequestMapping(value = "/areaByCityId", method = RequestMethod.GET)
    @ApiOperation(value = "根据城市id获取区县数据", httpMethod = "GET")
    public ResponseData<List<SysAreaDto>> findAreaByCityId(@RequestParam String cityId) {
        if (ParamUtil.isIllegal(cityId)) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        SysAreaDto sysAreaDto = new SysAreaDto();
        sysAreaDto.setArea("全部");
        List<SysAreaDto> resultArea = new ArrayList<>();
        resultArea.add(sysAreaDto);
        if(ApplicationConfig.SERVER_CONFIG.getSystem().isChooseArea()){
            resultArea.addAll(sysAreaService.list(MapUtils.build("father", cityId)).getData());
        }
        return ResponseData.buildSuccessResponse(resultArea);
    }

    @RequestMapping(value = "/openCities", method = RequestMethod.GET)
    @ApiOperation(value = "系统开通城市列表", httpMethod = "GET")
    public ResponseData<List<SysCityDto>> openCities(@RequestBody AdminParam adminParam) {
        String organizationUuid = adminParam.getAppid();
        if (StringUtils.isNotEmpty(adminParam.getUserAgentUuid())) {
            organizationUuid = adminParam.getUserAgentUuid();
        }
        if (StringUtils.isNotEmpty(adminParam.getUserCompanyUuid())) {
            organizationUuid = adminParam.getUserCompanyUuid();
        }
        Map<String, Object> build = MapUtils.build(4);
        build.put("organizationUuid", organizationUuid);
        build.put("status", 1);
        return sysCityService.getOpenCities(build);
    }
}
